package edu.towson.cosc.cosc455.dspenc5.compiler;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Scanner;

public class MyCompiler {
	/**
	 * Current token
	 */
	public static String token;
	/**
	 * All text from input file
	 */
	public static String code;
	/**
	 * Contains text after conversion from .lol to .html
	 */
	public static String htmlOutput;
	
	static MyLexicalAnalyzer lexer;
	static MySyntaxAnalyzer parser;
	static MySemanticAnalyzer generator;
	static ArrayList<String> parseTree;
	
	public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.AUDIO_END, MySemanticAnalyzer.AUDIO_END);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.BOLD_BEGIN, MySemanticAnalyzer.BOLD_BEGIN);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.AUDIO_BEGIN, MySemanticAnalyzer.AUDIO_BEGIN);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.COMMENT_BEGIN, MySemanticAnalyzer.COMMENT_BEGIN);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.COMMENT_END, MySemanticAnalyzer.COMMENT_END);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.DOC_BEGIN, MySemanticAnalyzer.DOC_BEGIN);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.DOC_END, MySemanticAnalyzer.DOC_END);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.TITLE_BEGIN, MySemanticAnalyzer.TITLE_BEGIN);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.TITLE_END, MySemanticAnalyzer.TITLE_END);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.BOLD_END, MySemanticAnalyzer.BOLD_END);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.HEAD_BEGIN, MySemanticAnalyzer.HEAD_BEGIN);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.HEAD_END, MySemanticAnalyzer.HEAD_END);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.ITALICS_BEGIN, MySemanticAnalyzer.ITALICS_BEGIN);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.ITALICS_END, MySemanticAnalyzer.ITALICS_END);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.LIST_END, MySemanticAnalyzer.LIST_END);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.LIST_BEGIN, MySemanticAnalyzer.LIST_BEGIN);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.ITEM_START, MySemanticAnalyzer.ITEM_START);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.NEWLINE, MySemanticAnalyzer.NEWLINE);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.ITEM_END, MySemanticAnalyzer.ITEM_END);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.VIDEO_BEGIN, MySemanticAnalyzer.VIDEO_BEGIN);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.VIDEO_END, MySemanticAnalyzer.VIDEO_END);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.PARAGRAPH_END, MySemanticAnalyzer.PARAGRAPH_END);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.PARAGRAPH_BEGIN, MySemanticAnalyzer.PARAGRAPH_BEGIN);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.VARIABLE_BEGIN, MySemanticAnalyzer.VARIABLE_BEGIN);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.VARIABLE_MIDDLE, MySemanticAnalyzer.VARIABLE_MIDDLE);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.VARIABLE_END, MySemanticAnalyzer.VARIABLE_END);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.VARIABLE_USE_BEGIN, MySemanticAnalyzer.VARIABLE_USE_BEGIN);
		MySemanticAnalyzer.conversionMap.put(LexicalTokens.VARIABLE_USE_END, MySemanticAnalyzer.VARIABLE_USE_END);
		
		token = "";
		htmlOutput = "";
		//Check that there is exactly one arg
		if(args.length !=1){
			System.err.println("There should be one and only one argument");
			System.exit(0);
		}
		
		//check that the file has the correct extension
		if(!args[0].endsWith(".lol")){
			System.err.println("The input file requires the file extension .lol");
			System.exit(0);
		}
		File input = new File(args[0]);
		Scanner sc = new Scanner(input);
		if(sc.hasNext()){
		code = sc.next(); //primes the code variable to avoid a null at start
		}
		else{
			System.err.println("The given file contains no text.");
			System.exit(0);
		}
		while(sc.hasNext()){
			code = code + " " + sc.next();
		}
		sc.close();
		
		parseTree = new ArrayList<String>();
		lexer = new MyLexicalAnalyzer(code);
		lexer.getNextToken();
		
		parser = new MySyntaxAnalyzer();
		parser.lolcode();
		
		generator = new MySemanticAnalyzer();
		generator.convertToHTML();
		
		
		/*
		 * Creates the file name by chopping off the extension of the given
		 * .lol filename and replacing it with .html
		 * The htmlOutput string is then written to the filename given above
		 */
		String filename = "";
		filename = args[0].substring(0, args[0].indexOf(".")+1)+"html";
		PrintWriter writer = new PrintWriter(filename, "UTF-8");
		writer.println(htmlOutput);
		writer.close();


	}

}

